// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import EventManager from "../../../Common/EventManager";
import Loader from "../../../Common/Loader";
import yyComponent from "../../../Common/yyComponent";
import { EventType } from "../../../GameSpecial/GameEventType";
import { mgrs, sers } from "../../system/OverallSystem ";
import { GAME_MODE } from "../game/gameMode_Json";

const {ccclass, property} = cc._decorator;

@ccclass
export default class zhutiListItem extends yyComponent{

   

    // onLoad () {}

    @property(cc.Sprite)
    icon:cc.Sprite = null

    @property(cc.Node)
    vicon:cc.Node = null


    @property(cc.Node)
    lockNode:cc.Node = null

    @property(cc.Node)
    xuanzhong:cc.Node = null

  


    start () {
        // this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this)
        this.on(EventType.DirectorEvent.pifugenghuan,this.updataView,this)
        // this.on(EventType.UIEvent.updataGamelist,this.updataView,this)
    }
    data = null
    type = 1
    ziyuanlujing = null
    lockData = null
    setDatas(data,lockData){
        let fenjie = data['储存'].split('_')
        this.type = +fenjie[1]
        this.data = +fenjie[0]
        if(data['资源路径']){
            this.ziyuanlujing = data['资源路径']
        }else{
            this.ziyuanlujing = 'zhutiIcon/'+this.type+'_'+this.data
        }
        this.lockData = lockData
        this.updataView()
    }

   
    updataView(){
        if(this.data==null)return
        let data = this.data

        let s = sers().userSrv.getZhutiData()
        if(s.indexOf(+this.data+'_'+this.type)>=0 || data == 0){
            this.vicon.active = false
            this.lockNode.active = true
        }else{
            let ds = this.lockData['获取方式']
            if(ds == '晋级'){
                this.vicon.getChildByName('duanwei').active = true
                this.vicon.getChildByName('duanwei').children[0].getComponent(cc.Label).string =  this.lockData['获取条件']
            }else if(ds == '金币'){
                this.vicon.getChildByName('jinbi').active = true
                this.vicon.getChildByName('jinbi').children[0].getComponent(cc.Label).string =  this.lockData['获取条件']
            }else if(ds =='签到'){
                this.vicon.getChildByName('qiandao').active = true
            }
        }

        if(this.type==1){
            let muqian = sers().userSrv.getZhutiBg()
            if(muqian==this.data){
                this.xuanzhong.active = true
                this.lockNode.active = false
            }else{
                this.xuanzhong.active = false
                this.lockNode.active = true
            }
        }

        if(this.type==2){
            let muqian = sers().userSrv.getZhuti()
            if(muqian==this.data){
                this.vicon.active = false
                this.xuanzhong.active = true
                this.lockNode.active = false
            }else{
                this.xuanzhong.active = false
                this.lockNode.active = true
            }
        }


        Loader.loadBundleRes('home', 'image/'+this.ziyuanlujing, (res) => {
        //    cc.log(res)
            if(!this || !this.node)return
            this.icon.spriteFrame = new cc.SpriteFrame(res)
        })
    }
    jieduanName = ['小白', '新手', '入门', '熟练', '精英', '高手', '专业', '大师', '宗师', '雀圣', '雀神']

    onTouchStart(){
        mgrs().soundMgr.playBtnSound();

        let str = this.type==1 ? "背景":"牌色"
        if(this.vicon.active){


            let ds = this.lockData['获取方式']
            if(ds == '晋级'){
                let UserData = sers().userSrv.getUserData()
                let index = this.jieduanName.indexOf(this.lockData['获取条件'])
                // if((window['tt']||window['wx']) && UserData.maxLevel< index+1){
                if(UserData.maxLevel < index+1 && (window['tt']||window['wx'])){
                    sers().uiSrv.showMessage(`达到${this.lockData['获取条件']}段位，解锁该皮肤`)
                    return 
                }
                let s = sers().userSrv.getZhutiData()
                s.push(+this.data+'_'+this.type)
                sers().userSrv.setZhutiData(s)
                EventManager.emit(EventType.DirectorEvent.pifugenghuan)
               
            }else if(ds == '金币'){

                if(+sers().userSrv.coin < +this.lockData['获取条件']){
                    sers().uiSrv.showMessage(`金币不足`)
                    return
                }
                sers().userSrv.addCoin(- +this.lockData['获取条件'])
                let s = sers().userSrv.getZhutiData()
                s.push(+this.data+'_'+this.type)
                sers().userSrv.setZhutiData(s)
                EventManager.emit(EventType.DirectorEvent.pifugenghuan)
                EventManager.emit(EventType.UIEvent.SignInList)

            }else if(ds =='签到'){
                sers().uiSrv.showMessage('完成签到之后即可获得')
            }else{
                sers().platformSrv.platformApi.loadAndShowVideoAd(() => {
                    let s = sers().userSrv.getZhutiData()
                    s.push(+this.data+'_'+this.type)
                    sers().userSrv.setZhutiData(s)
                    EventManager.emit(EventType.DirectorEvent.pifugenghuan)
    
                }, (err) => {
                }, null, {
                    scene: '皮肤解锁',
                    event: '解锁'+str+this.data,
                  },'common_video');
            }
          
        }else{
            // sers().userSrv.setGameList(this.data.gameType)
            // mgrs().gameMgr.enterMode(this.data.gameType);
            // window['guanqiaMODE'] = this.data.id
            // window['guanqiaMODE_name'] = this.data.name
            // window['guanqiaMODE_key'] = this.data.key
            // mgrs().gameMgr.enterMode(GAME_MODE.MAJIANGLEVEL);
            cc.log(this.type)
            if(this.type==1){
                sers().userSrv.setZhutiBg(this.data)
            }else{
                sers().userSrv.setZhuti(this.data)
            }

            EventManager.emit(EventType.DirectorEvent.pifugenghuan)
        }
    }

    // update (dt) {}
}
